package com.it.you.base.designmode.singleton;

import lombok.SneakyThrows;

import java.io.File;
import java.io.FileWriter;

public class Logger {

    private FileWriter writer;

    @SneakyThrows
    public Logger() {
        File file = new File("/Users/youhui/Documents/log.txt");
        writer = new FileWriter(file, true); //true表示追加写入
    }

    @SneakyThrows
    public void log(String message) {
        writer.write(message);
    }

    @SneakyThrows
    public void log2(String message) {
        synchronized (this) {//this 对象锁 多线程安全问题 导致日志覆盖
            writer.write(message);
        }
    }

    @SneakyThrows
    public void log3(String message) {
        synchronized (Logger.class) {//类锁 没有线程问题
            writer.write(message);
        }
    }

}
